﻿using CavinatorSite.CavinatorService;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace CavinatorSite
{
    public partial class Jogo : Page
    {
        private CavinatorServiceClient _cavinatorService;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                _cavinatorService = new CavinatorServiceClient();
                Session.Add("Service", _cavinatorService);
                StartAndNext(new Protocol());
            }
            else
            {
                _cavinatorService = (CavinatorServiceClient)Session["Service"];
            }
        }

        //TODO Refatorar Nomes de Variaveis
        private void StartAndNext(Protocol item)
        {
            if (item.Info == null)
            {
                var serviceData = _cavinatorService.GetNextState(null);
                FillPageFields(serviceData);
            }
            else
            {
                //If item its false fill the Fields
                if (!item.IsAnswer)
                {
                    var serviceAnswer = _cavinatorService.GetNextState(new Protocol
                     {
                         IsAnswer = item.IsAnswer,
                         IdNode = item.IdNode,
                         Info = item.Info,
                         Answers = item.Answers
                     });
                    FillPageFields(serviceAnswer);
                }
                //fill the box if the item its true.

            }
        }

        protected void BtnNextClick(object sender, EventArgs e)
        {
            var question = new Question
            {
                Value = lblAnswer.Text,
                Id = Convert.ToInt32(hdIdQuestion.Value)
            };

            var lAnwer = new List<Answer>
            {
                new Answer
                {
                    Question = question,
                    AnswerValue = ddlrespostas.SelectedValue,
                    Id = ddlrespostas.SelectedIndex,
                    IdItem = ddlrespostas.SelectedIndex,
                    IdQuestion = Convert.ToInt32(hdIdQuestion.Value)
                }
                
            };
            var sendProtocol = new Protocol
                               {
                                   Info = lblAnswer.Text,
                                   IsAnswer = false,
                                   Answers = lAnwer
                               };
            StartAndNext(sendProtocol);




        }

        private void FillPageFields(Protocol serviceData)
        {
            ddlrespostas.Items.Clear();
            lblAnswer.Text = serviceData.Info;
            hdIdQuestion.Value = serviceData.IdQuestion.ToString();
         

            if (serviceData.IsAnswer)
            {
                lblAnswer2.Text = serviceData.Answers[0].AnswerValue;//Mostra a resposta na modal
                ClientScript.RegisterStartupScript(GetType(), "alerta", "$('#myModal').modal('show')", true);
                btnNext.Visible = false;
                lblResp.Visible = false;
                ddlrespostas.Visible = false;
            }
            else if (serviceData.Answers.Count == 0)
            {
                ClientScript.RegisterStartupScript(GetType(), "alerta", "$('#myModalErro').modal('show')", true);
                btnNext.Visible = false;
                lblResp.Visible = false;
                ddlrespostas.Visible = false;
            }
            else
            {
                foreach (var answer in serviceData.Answers)
                {
                    ddlrespostas.Items.Add(new ListItem(answer.AnswerValue));
                }
            }
        }

        protected void btnNovoItem_Click(object sender, EventArgs e)
        {
            Response.Redirect("~/NovoItem.aspx");
        }
    }
}